package com.expressage.controller;

import com.expressage.po.Role;
import com.expressage.service.RoleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jws.WebParam;
import java.util.List;

@Controller
@RequestMapping("/jspafter")
public class RoleController {
    @Autowired
    RoleService roleService;
    Logger logger = Logger.getLogger(RoleController.class);

    @RequestMapping("/toaddrole")
    public String toadd(){
        return "jspafter/addrole";
}
    @RequestMapping("/addrole")
    public String addrole(Role role){
        logger.info("进入添加角色控制器");
          boolean falg= roleService.add(role);
          logger.info(falg);
          return "redirect:rolelist.action";
    }

    @RequestMapping("/rolelist")
    public String Rolelist(Model model){
        logger.info("进入角色列表控制器");
        List<Role> list = roleService.findAll();
        model.addAttribute("rolelist",list);
        for (Role ro :
                list) {
            logger.info(ro.getRname());
        }
        return "jspafter/rolelist";
    }

    @RequestMapping("/rolelistone")
    public String findByid(int rid,Model model){
        logger.info("进入查询单个角色的控制器");
        Role role =  roleService.findByid(rid);
        model.addAttribute(role);
        logger.info(role);
        return "jspafter/rolelistone";
    }

    @RequestMapping("/deleterole")
    public String deleteRole(int[] array){
        if(array.length>0){
              boolean falg = roleService.delete(array);
              logger.info(falg?true:false);
        }
        return "redirect:rolelist.action";
    }

    @RequestMapping("/toupdaterole")
    public String updateRole(int rid,Model model){
        logger.info("去修改角色");
      Role role =  roleService.findByid(rid);
      model.addAttribute("role",role);
      return "jspafter/updaterole";
    }

    @RequestMapping("/updaterole")
    public String update(Role role){
    logger.info("修改角色中");
    boolean flag = roleService.update(role);
    return "redirect:rolelist.action";
   }

}
